package com.ymttest.business.service.shenqi;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import com.google.common.collect.Lists;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.PlaceOrderReq;
import com.shop2cn.iapi.sqoptrade.parameter.vo.CouponVo;
import com.shop2cn.iapi.sqoptrade.parameter.vo.OrderGroupVo;
import com.shop2cn.iapi.sqoptrade.parameter.vo.OrderItemVo;
import com.shop2cn.iapi.sqoptrade.parameter.vo.OrderVo;
import com.shop2cn.iapi.sqoptrade.parameter.vo.PrepaidCardVo;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.ymatou.iapi.cardservice.service.BindingCardCall;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.cardservice.CardServiceMapper;
import com.ymttest.utils.EnvSetup;

public class PrepaidCardService {

	public static CardServiceMapper cardServiceMapper = SqlMapperFactory
			.getCardServiceMapper();

	private static String cardId_sit1_1 = "10000003";

	private static String cardId_sit1_2 = "10000004";

	private static String cardId_uat_1 = "10000003";

	private static String cardId_uat_2 = "10000004";

	public static String cardId1, cardId2;

	static {

		if (EnvSetup.getEnv().equals("sit1")) {

			cardId1 = cardId_sit1_1;
			cardId2 = cardId_sit1_2;

		} else {
			cardId1 = cardId_uat_1;
			cardId2 = cardId_uat_2;
		}
	}

	public static void bindingCard(int userId, String userName) {

		//解绑卡
		cardServiceMapper.updateCardAsOrigByCardId(cardId1);

		BindingCardCall bindingCardCall = new BindingCardCall();

		bindingCardCall.call(cardId1, userId, userName, "auto");

	}

	public static void bindingCard(int userId, String userName, String cardId) {

		//解绑卡
		cardServiceMapper.updateCardAsOrigByCardId(cardId);

		BindingCardCall bindingCardCall = new BindingCardCall();

		bindingCardCall.call(cardId, userId, userName, "auto");

	}

	/**
	 * 查预付卡流水
	 * @param userId
	 */
	public static BigDecimal getCardEntry(int userId, String bizNo) {

		//预付卡流水
		List<Map<String, Object>> res = cardServiceMapper
				.selectCardEntryByUserId(userId, bizNo);

		if (CollectionUtils.isEmpty(res)) {
			return BigDecimal.ZERO;
		}

		return res.stream().map(m -> (BigDecimal) m.get("Amount"))
				.reduce(BigDecimal::add).get();

	}
	/**
	 * 预付卡 下单
	 * @return
	 * @throws Exception 
	 */
	public static TradingServiceResp placeOrder(boolean isUserCoupon) throws Exception {

		AgentProductBean productBean = DGSQBuildProxyChainCallService
				.createProxychain(500002490, 0, false);
		
		PlaceOrderReq placeorderBean = SqTradingCallService
				.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
						productBean, 500002490);

		OrderItemVo orderItemVo1 = SqTradingCallService.createOrderItemVo(productBean.getProductId(), productBean.getCatalogList().get(0));

		OrderVo orderVo1 = SqTradingCallService.createOrderVo(Arrays.asList(productBean));

		List<OrderVo> orderVoList = placeorderBean.getOrderGroups().get(0)
				.getOrders();

		orderVoList.get(0).getOrderItems().add(orderItemVo1);

		orderVoList.add(orderVo1);

		OrderGroupVo orderGroupVo2 = SqTradingCallService.createOrderGroupVo(productBean, 500002490);

		//OrderVo orderVo2 = PlaceOrderService.getDefaultOrderVo(userId);

		//orderGroupVo2.getOrders().add(orderVo2);

		placeorderBean.getOrderGroups().add(orderGroupVo2);

		PrepaidCardVo prepaidCardVo = new PrepaidCardVo();

		prepaidCardVo.setPrepaidAmount(BigDecimal.TEN);
		prepaidCardVo.setPriority(1);
		prepaidCardVo.setPrepaidCardNo(cardId1);

		PrepaidCardVo prepaidCardVo2 = new PrepaidCardVo();

		prepaidCardVo2.setPrepaidAmount(new BigDecimal(30));
		prepaidCardVo2.setPriority(2);
		prepaidCardVo2.setPrepaidCardNo(cardId2);

		placeorderBean.setPrepaidCardVos(Arrays.asList(prepaidCardVo,
				prepaidCardVo2));

		if (!isUserCoupon) {

			placeorderBean.setYmatouCoupon(null);

			placeorderBean.getOrderGroups().forEach(
					group -> group.setSellerCoupon(null));

		}

		return SqTradingCallService.callPlaceOrderService(placeorderBean);

	}

	/**
	 * 预付卡 下单
	 * @return
	 * @throws Exception 
	 */
	public static TradingServiceResp placeOrder2(boolean isUserCoupon) throws Exception {

		AgentProductBean productBean = DGSQBuildProxyChainCallService
				.createProxychain(500002490, 0, false);
		
		PlaceOrderReq placeorderBean = SqTradingCallService
				.buildDGSQStandPlaceOrderReq(4321, BigDecimal.ZERO, 20238699,
						productBean, 500002490);

		long userId = placeorderBean.getUserId();

		//买手1 订单组
		OrderGroupVo groupVo1 = placeorderBean
				.getOrderGroups().get(0);

		OrderItemVo orderItemVo1 = SqTradingCallService
				.createOrderItemVo(productBean.getProductId(),
						productBean.getCatalogList().get(0));
		OrderItemVo orderItemVo2 = SqTradingCallService
				.createOrderItemVo(productBean.getProductId(),
						productBean.getCatalogList().get(1));

		groupVo1.getOrders().get(0).getOrderItems()
				.addAll(Lists.newArrayList(orderItemVo1, orderItemVo2));
		
		AgentProductBean productBea2 = DGSQBuildProxyChainCallService
				.createProxychain(500002490, 0, false);

		OrderVo OrderVo1 = SqTradingCallService
				.createOrderVo(Arrays.asList(productBea2));

		groupVo1.getOrders().add(OrderVo1);

		BigDecimal totalAmount = BigDecimal.ZERO;

		for (OrderVo order : groupVo1.getOrders()) {

			totalAmount = order
					.getOrderItems()
					.stream()
					.map(item ->

					item.getProductPrice()
							.multiply(new BigDecimal(item.getProductNum()))
							.add(item.getFreight())

					).reduce(BigDecimal.ZERO, BigDecimal::add).add(totalAmount);
		}

		PrepaidCardVo prepaidCardVo = new PrepaidCardVo();

		BigDecimal card1 = BigDecimal.ZERO;
		BigDecimal card2 = BigDecimal.ZERO;

		card1 = totalAmount.subtract(new BigDecimal(100)).subtract(
				BigDecimal.TEN);

		card2 = totalAmount.subtract(new BigDecimal(100)).subtract(card1);

		prepaidCardVo.setPrepaidAmount(card1);

		prepaidCardVo.setPrepaidCardNo(PrepaidCardService.cardId1);

		PrepaidCardVo prepaidCardVo2 = new PrepaidCardVo();

		prepaidCardVo2.setPrepaidAmount(card2);

		prepaidCardVo2.setPrepaidCardNo(PrepaidCardService.cardId2);

		placeorderBean.setPrepaidCardVos(Arrays.asList(prepaidCardVo,
				prepaidCardVo2));

		if (isUserCoupon) {

			CouponVo sellerCoupon1 = SqTradingCallService.createSellerCouponVo(
					userId, 500002490L, new BigDecimal(30), BigDecimal.TEN);

			groupVo1.setSellerCoupon(sellerCoupon1);

		}

		return SqTradingCallService.callPlaceOrderService(placeorderBean);

	}
	public static void main(String[] args) {

	}
}
